package Servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Clases.Turno;
import Datos.TurnoAdapter;

public class Final_IUDTurno extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public Final_IUDTurno() {
        super();
        
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int id = Integer.parseInt(request.getParameter("idTurno"));
			TurnoAdapter cTurno = new TurnoAdapter();
			cTurno.Delete(id);
			response.sendRedirect("turnos.jsp");
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url="";
		try
		{
			int opc = Integer.valueOf(request.getParameter("TipoAccion"));
			TurnoAdapter cTurno = new TurnoAdapter();			
			Turno aTurno = new Turno();
			aTurno.setCliente(request.getParameter("regCliente"));
			aTurno.setHorario(request.getParameter("regHora"));
			aTurno.setFecha(request.getParameter("regFecha"));
			aTurno.setIdProfesional(Integer.valueOf(request.getParameter("regProfesional")));
			aTurno.setMotivo(request.getParameter("regMotivo"));
			aTurno.setObservaciones(request.getParameter("regObservaciones"));
			String resp = "";
			switch(opc)
			{
				case 0:
					//Inserta
					if(!cTurno.Insert(aTurno)) url="index.jsp?error=2";
					else url="turnos.jsp";
					break;
				case 1:
					//Actualiza
					aTurno.setId(Integer.parseInt(request.getParameter("idTurno")));
					if(!cTurno.Update(aTurno)) url="index.jsp?error=2";
					else url="turnos.jsp";
					break;					
			}
			if(resp.equals(""))
			{
				//Operacion Exitosa
				response.sendRedirect(url);
			} else {
				//Error
				response.sendRedirect("turnos.jsp?Error="+resp);
			}
		} catch(Exception ex)
		{
			response.sendRedirect(url+"?Error="+ex.getMessage());
		}
	
	}

}
